﻿using MyTRCP.Common;

namespace MyTRCP.Common.Models
{
    public class DevSettings : BitWalkableObj
    {
        public byte ChannelA { get; set; } //b0[7:4]
        public byte ChannelB { get; set; } //b0[3:0]
        public bool Scan { get; set; } //b1[7]
        public bool AghfpCallMode { get; set; }//b1[6]
        public DoubleChannelTypeEnum DoubleChannel { get; set; }//b1[5:4]
        public byte SquelchLevel { get; set; }//b1[3:0]
        public bool TailElim { get; set; }//b2[7]
        public bool AudioRelayEnabled { get; set; }//b2[6]
        public bool AutoPowerOn { get; set; }//b2[5]
        public bool KeepAghfpLink { get; set; }//b2[4]
        public byte MicGain { get; set; }//b2[3:1]
        public byte TxHoldTime { get; set; } //b2[0]|b3[7:5]
        public byte TxTimeLimit { get; set; }//b3[4:0]
        public byte LocalSpeaker { get; set; }//b4[7:6]
        public byte BtMicGain { get; set; }//b4[5:3]
        public bool AdaptiveResponse { get; set; }//b4[2]
        public bool DisTone { get; set; }//b4[1]
        public bool PowerSavingMode { get; set; }//b4[0]
        public byte AutoPowerOff { get; set; }//b5[7:5]
        public byte AutoShareLocCh { get; set; }//b5[4:0]
        public byte HmSpeaker { get; set; }//b6[7:6]
        public byte PositioningSystem { get; set; }//b6[5:2]
        public byte TimeOffset { get; set; }//b6[1:0]|b7[7:4]
        public bool UseFreqRange2 { get; set; }//b7[3]
        public bool PttLock { get; set; }//b7[2]
        public bool LeadingSyncBitEnabled { get; set; }//b7[1]
        public bool PairingAtPowerOn { get; set; }//b7[0]
        public byte ScreenTimeout { get; set; }//b8[7:3]
        public DoubleChannelTypeEnum VfoX { get; set; }//b8[2:1]
        public bool ImperialUnit { get; set; }//b8[0]
        //channelA  /16   b9[7:4]
        //channelB  /16   b9[3:0]
        public byte WxMode { get; set; }//b10[7:6]
        public byte NOAACh { get; set; }//b10[5:2]
        public PowerTypeEnum Vfo1TxPowerX { get; set; }//b10[1:0]
        public PowerTypeEnum Vfo2TxPowerX { get; set; }//b11[7:6]
        public bool DisDigitalMute { get; set; }//b11[5]
        public bool SignalingECCEnabled { get; set; }//b11[4]
        public bool ChDataLock { get; set; }//b11[3]
        public byte P { get; set; }//b11[2:0]
        public int Vfo1ModFreqX { get; set; }//b12:b15
        public int Vfo2ModFreqX { get; set; }//b16:b19
        public DevSettings(byte[] data) : base(data)
        {
        }
        public override string ToString()
        {
            return $"channelA:{ChannelA};channelB:{ChannelB};scan:{Scan};aghfpCallMode:{AghfpCallMode};doubleChannel:{DoubleChannel};squelchLevel:{SquelchLevel};tailElim:{TailElim};audioRelayEn:{AudioRelayEnabled};autoPowerOn:{AutoPowerOn};keepAgfhpLink:{KeepAghfpLink};micGain:{MicGain};txHoldTime:{TxHoldTime};txTimeLimit:{TxTimeLimit};localSpeaker:{LocalSpeaker};btMicGain:{BtMicGain};adaptiveResponse:{AdaptiveResponse};disTone:{DisTone};powerSavingMode:{PowerSavingMode};autoPowerOff:{AutoPowerOff};autoShareLocCh:{AutoShareLocCh};hmSpeaker:{HmSpeaker};positioningSystem:{PositioningSystem};timeOffset:{TimeOffset};useFreqRange2:{UseFreqRange2};pttLock:{PttLock};leadingSyncBitEn:{LeadingSyncBitEnabled};pairingAtPowerOn:{PairingAtPowerOn};screenTimeout:{ScreenTimeout};vfoX:{VfoX};imperialUnit:{ImperialUnit};wxMode:{WxMode};noaaCh:{NOAACh};vfo1TxPowerX:{Vfo1TxPowerX};vfo2TxPowerX:{Vfo2TxPowerX};disDigitalMute:{DisDigitalMute};signalingEccEn:{SignalingECCEnabled};chDataLock:{ChDataLock};p:{P};vfo1ModFreqX:{Vfo1ModFreqX};vfo2ModFreqX:{Vfo2ModFreqX};";
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBits(ChannelA, 4)
                .WriteBits(ChannelB, 4)
                .WriteBit(Scan)
                .WriteBit(AghfpCallMode)
                .WriteBits((byte)DoubleChannel, 2)
                .WriteBits(SquelchLevel, 4)
                .WriteBit(TailElim)
                .WriteBit(AudioRelayEnabled)
                .WriteBit(AutoPowerOn)
                .WriteBit(KeepAghfpLink)
                .WriteBits(MicGain, 3)
                .WriteBits(TxHoldTime, 4)
                .WriteBits(TxTimeLimit, 5)
                .WriteBits(LocalSpeaker, 2)
                .WriteBits(BtMicGain, 3)
                .WriteBit(AdaptiveResponse)
                .WriteBit(DisTone)
                .WriteBit(PowerSavingMode)
                .WriteBits(AutoPowerOff, 3)
                .WriteBits(AutoShareLocCh, 5)
                .WriteBits(HmSpeaker, 2)
                .WriteBits(PositioningSystem, 4)
                .WriteBits(TimeOffset, 6)
                .WriteBit(UseFreqRange2)
                .WriteBit(PttLock)
                .WriteBit(LeadingSyncBitEnabled)
                .WriteBit(PairingAtPowerOn)
                .WriteBits(ScreenTimeout, 5)
                .WriteBits((byte)VfoX, 2)
                .WriteBit(ImperialUnit)
                .WriteBits(ChannelA >> 4, 4)
                .WriteBits(ChannelB >> 4, 4)
                .WriteBits(WxMode, 2)
                .WriteBits(NOAACh, 4)
                .WriteBits((byte)Vfo1TxPowerX, 2)
                .WriteBits((byte)Vfo2TxPowerX, 2)
                .WriteBit(DisDigitalMute)
                .WriteBit(SignalingECCEnabled)
                .WriteBit(ChDataLock)
                .WriteBits(P, 3)
                .WriteBits(Vfo1ModFreqX, 32)
                .WriteBits(Vfo2ModFreqX, 32);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 20)
                throw new ArgumentException("字节数组长度过小（20）", nameof(bw));
            ChannelA = (byte)bw.ReadBits(4);
            ChannelB = (byte)bw.ReadBits(4);
            Scan = bw.ReadBit();
            AghfpCallMode = bw.ReadBit();
            DoubleChannel = (DoubleChannelTypeEnum)bw.ReadBits(2);
            SquelchLevel = (byte)bw.ReadBits(4);
            TailElim = bw.ReadBit();
            AudioRelayEnabled = bw.ReadBit();
            AutoPowerOn = bw.ReadBit();
            KeepAghfpLink = bw.ReadBit();
            MicGain = (byte)bw.ReadBits(3);
            TxHoldTime = (byte)bw.ReadBits(4);
            TxTimeLimit = (byte)bw.ReadBits(5);
            LocalSpeaker = (byte)bw.ReadBits(2);
            BtMicGain = (byte)bw.ReadBits(3);
            AdaptiveResponse = bw.ReadBit();
            DisTone = bw.ReadBit();
            PowerSavingMode = bw.ReadBit();
            AutoPowerOff = (byte)bw.ReadBits(3);
            AutoShareLocCh = (byte)bw.ReadBits(5);
            HmSpeaker = (byte)bw.ReadBits(2);
            PositioningSystem = (byte)bw.ReadBits(4);
            TimeOffset = (byte)bw.ReadBits(6);
            UseFreqRange2 = bw.ReadBit();
            PttLock = bw.ReadBit();
            LeadingSyncBitEnabled = bw.ReadBit();
            PairingAtPowerOn = bw.ReadBit();
            ScreenTimeout = (byte)bw.ReadBits(5);
            VfoX = (DoubleChannelTypeEnum)bw.ReadBits(2);
            ImperialUnit = bw.ReadBit();
            ChannelA |= (byte)(bw.ReadBits(4) << 4);
            ChannelB |= (byte)(bw.ReadBits(4) << 4);
            WxMode = (byte)bw.ReadBits(2);
            NOAACh = (byte)bw.ReadBits(4);
            Vfo1TxPowerX = (PowerTypeEnum)bw.ReadBits(2);
            Vfo2TxPowerX = (PowerTypeEnum)bw.ReadBits(2);
            DisDigitalMute = bw.ReadBit();
            SignalingECCEnabled = bw.ReadBit();
            ChDataLock = bw.ReadBit();
            P = (byte)bw.ReadBits(3);
            Vfo1ModFreqX = (int)bw.ReadBits(32);
            Vfo2ModFreqX = (int)bw.ReadBits(32);
            return bw;
        }
    }
}